//
//  AreaSettingVC.swift
//  WeFamily
//
//  Created by 崔世强 on 2020/7/7.
//  Copyright © 2020 Meak Tsui. All rights reserved.
//

import UIKit
//import MTSDK


extension NSDictionary {
    @objc func name() -> String? {

        return self["name"] as? String
    }
}

class AreaSettingVC: UITableViewController {
    
    @IBOutlet weak var doneBtn: UIBarButtonItem!
    
    private var areas: [[String: Any]] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if !areas.isEmpty {
            return
        }
//        let path = Dir.application("area.plist")
//        if let array = NSArray(contentsOfFile: path) as? [[String: Any]] {
//            areas = array
//        }
    }
    var preSelected:(String,String)?
    
    var province: String = ""
    var area: String = ""
    
    var doneCallback:((_ province:String,_ city:String) -> Void)?

    @IBAction func doneBtnClick(_ sender: Any) {
        doneCallback?(province,area)
        self.dismiss(animated: true, completion: nil)
    }
    
    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return areas.count
    }

    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
        let province = areas[indexPath.row]
        let level = province["level"] as! String
        let name = province["name"] as! String
        let children = province["children"] as? [[String:Any]] ?? []
        cell.textLabel?.text = name
        if level == "2" {
            if let selectedName = preSelected?.1,selectedName == name {
                cell.detailTextLabel?.text = NSLocalizedString("已选择", comment: "已选择")
            }
            cell.accessoryType = .none
        } else if level == "1", children.isEmpty {
            if let selectedName = preSelected?.0,selectedName == name {
                cell.detailTextLabel?.text = NSLocalizedString("已选择", comment: "已选择")
            }
            cell.accessoryType = .disclosureIndicator
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let province = areas[indexPath.row]
        let level = province["level"] as! String
        let name = province["name"] as! String
        let areas = province["children"] as? [[String:Any]] ?? []
        if level == "2" {
            let cell = tableView.cellForRow(at: indexPath)
            cell?.accessoryType = .checkmark
            area = name
            doneBtn.isEnabled = true
            tableView.deselectRow(at: indexPath, animated: true)
            return
        } else if level == "1",areas.isEmpty {
            let cell = tableView.cellForRow(at: indexPath)
            cell?.accessoryType = .checkmark
            self.province = name
            doneBtn.isEnabled = true
            tableView.deselectRow(at: indexPath, animated: true)
            return
        }
        
        let vc = UIStoryboard(name: "AreaSetting", bundle: Bundle.main).instantiateViewController(withIdentifier: "AreaSettingVC") as! AreaSettingVC
        vc.areas = areas
        vc.province = name
        vc.doneCallback = self.doneCallback
        self.navigationController?.pushViewController(vc, animated: true)
    }

}
